<?php


/**
 * Form generating functions <br /><br />
 *
 * This file contains functions for auto generating HTML form.
 * Usage of individual function is described below.
 *
 * @package OWL
 * @subpackage Format
 * @since 1.0
 * @author KeiMOon
 */

class Owl_Form {

/**
 * Text box generating function
 *
 * @since 1.0
 * @author KeiMOon
 *
 * @param array $attr This parameter is an array containing following key: <br />
 * <b>type</b> : The type of textbox is normal textbox or
 * password textbox. Default value is <u>text</u>, may be <u>password</u>) <br />
 * <b>name</b> : The name of the text box. This field is required <br />
 * <b>value</b> : The value of the text box. This field is optional <br />
 * <b>id</b> : The ID of the text box. This field is optional <br />
 * <b>class</b> : The class of the text box. This field is optional. Default
 * value is <u>form-textbox</u> <br />
 * <b>maxlength</b> : The maximum input length of the text box. This field is optional <br />
 * <b>disabled</b> : Text box is disabled or not. Default value is <u>false</u>,
 * may be <u>true</u>) <br />
 * <b>readOnly</b> : Textbox is read-only or not. Default value is <u>false</u>,
 * may be <u>true</u>) <br />
 * <b>description</b> : The description of the text box. This field is optional. Description will
 * be display under the form element. <br />
 * <b>toString</b> : Print to a string instead
 * @return bool
 */

    static function print_textbox($attr) {
        if (empty($attr["name"])) {
            return false;
        }
        if (empty($attr["type"]) || $attr["type"]!="text" && $attr["type"]!= "password") {
            $attr["type"]="text";
        }
        if (empty($attr["class"])) {
            $attr["class"] = "form-textbox";
        }

        $string = '<input type="' . $attr["type"] . '"
                          name="' . $attr["name"] . '" ';

        if (isset($attr["value"])) { 
            $string .= 'value="' . $attr["value"] . '" ';
        }

        if (isset($attr["id"])) {
            $string .= 'id="' . $attr["id"] . '" ';
        }

        if (isset($attr["class"])) { 
            $string .= 'class="' . $attr["class"] . '" ';
        } 

        if (isset($attr["maxlength"])) { 
            $string .= 'maxlength="' . $attr["maxlength"] . '" ';
        } 

        if ($attr["disabled"]===true) {
            $string .= 'disabled ';
        } 

        if ($attr["readOnly"]===true) { 
            $string .= 'readOnly ';
        }
        $string .= ' />';
        if (!empty($attr["description"])) { 
            $string .= '<div class="form-description">' . $attr["description"] . '</div>';
        } 
        if ($attr["toString"]) {
            return $string;
        }
        else {
            echo $string;
        }
} //End print_textbox


    /**
    * Combo box generating function
    *
    * @since 1.0
    * @author KeiMOon
    *
    * @param array $attr This parameter is an array containing following key: <br />
    * <b>options</b> : This is an array of options. Format: option_value=>option_name.
    * This field is required. <br />
    * <b>name</b> : The name of the combo box. This field is required <br />
    * <b>value</b> : The value of the combo box. This field is optional <br />
    * <b>id</b> : The ID of the combo box. This field is optional <br />
    * <b>class</b> : The class of the combo box. This field is optional. Default
    * value is <u>form-combobox</u> <br />
    * <b>disabled</b> : Combo box is disabled or not. Default value is <u>false</u>,
    * may be <u>true</u>) <br />
    * <b>description</b> : The ID of the combo box. This field is optional. Description will
    * be display under the form element. <br />
    * <b>toString</b> : Print to a string instead
    * @return bool
    */

    static function print_combo_box($attr) {
        if (!isset($attr["name"]) || !is_array($attr["options"])) {
            return false;
        }
        if (empty($attr["class"])) {
            $attr["class"] = "form-combobox";
        }

        $string  = '<select name="' . $attr["name"] . '" ';
        if (isset($attr["id"])) {
            $string .= 'id="' . $attr["id"] . '" ';
        }
        if (isset($attr["class"])) {
            $string .= 'class="' . $attr["class"] . '" ';
        }
        if ($attr["disabled"]===true) {
            $string .= 'disabled ';
        }
        $string .= ">";
                        
        foreach ($attr["options"] as $option_value=>$option_name) {
            $string .= '<option value="' . $option_value . '" ';
            if ($attr["value"]==$option_value) {
                $string .= 'selected="selected"';
            } 
            $string .= '>' . $option_name . '</option>';
        }

        $string .= '</select>';
        if (!empty($attr["description"])) {
            $string .= '<div class="form-description">' . $attr["description"] . '</div>';
        }
        if ($attr["toString"]) {
            return $string;
        }
        else {
            echo $string;
        }
    }//End print_combo_box

    /**
     * Check box generating function
     *
     * @since 1.0
     * @author KeiMOon
     *
     * @param array $attr This parameter is an array containing following key: <br />
     * <b>name</b> : The name of the check box. This field is required <br />
     * <b>value</b> : The value of the check box. Check box will be check if
     * the value is <u>"on"</u> or <u>"true"</u>. This field is optional <br />
     * <b>id</b> : The ID of the check box. This field is optional <br />
     * <b>class</b> : The class of the check box. This field is optional. Default
     * value is <u>form-checkbox</u> <br />
     * <b>disabled</b> : check box is disabled or not. Default value is <u>false</u>,
     * may be <u>true</u>) <br />
     * <b>description</b> : The ID of the check box. This field is optional. Description will
     * be display under the form element. <br />
     * <b>toString</b> : Print to a string instead
     * @return bool
     */

    static function print_check_box($attr) {
        if (!isset($attr["name"])) {
            return false;
        }
        if (empty($attr["class"])) {
            $attr["class"] = "form-checkbox";
        }
        

        $string = '<input type="checkbox"
                          name="' . $attr["name"] . '" ';
        if (isset($attr["id"])) {
            $string .= 'id="' . $attr["id"] . '" ';
        }
        if (isset($attr["class"])) {
            $string .= 'class="' . $attr["class"] . '" ';
        }
        if ($attr["disabled"]===true) {
            $string .= 'disabled ';
        }
        if ($attr["value"]=="on" || $attr["value"]=="true") { 
            $string .= 'checked="checked"';
        } 
        $string .= ' />';
        
        if (!empty($attr["description"])) {
            $string .= '<div class="form-description">' . $attr["description"] . '</div>';
        }
        if ($attr["toString"]) {
            return $string;
        }
        else {
            echo $string;
        }
    }//End print_check_box

    /**
     * Radio button generating function
     *
     * @since 1.0
     * @author KeiMOon
     *
     * @param array $attr This parameter is an array containing following key: <br />
     * <b>options</b> : This is an array of options. Format: option_value=>option_name.
     * This field is required. <br />
     * <b>name</b> : The name of the radio button. This field is required <br />
     * <b>value</b> : The value of the radio button. This field is optional <br />
     * <b>id</b> : The ID of the radio button. This field is optional <br />
     * <b>class</b> : The class of the radio button. This field is optional. Default
     * value is <u>form-radio</u> <br />
     * <b>disabled</b> : Radio button is disabled or not. Default value is <u>false</u>,
     * may be <u>true</u>) <br />
     * <b>description</b> : The ID of the radio button. This field is optional. Description will
     * be display under the form element. <br />
     * <b>toString</b> : Print to a string instead
     * @return bool
     */

    static function print_radio($attr) {
        if (!isset($attr["name"]) || !is_array($attr["options"])) {
            return false;
        }
        if (!isset($attr["value"])) {
            reset($attr["options"]);
            $attr["value"] = key($attr["options"]);
        }
        if (empty($attr["class"])) {
            $attr["class"] = "form-radio";
        }
        $string = "";
        foreach ($attr["options"] as $option_value=>$option_name) { 
            $string .= '<span class="form-radio-label">' . $option_name . '</span> ';
            $string .= '<input type="radio"
                               name="' . $attr["name"] . '"
                               value="' . $option_value . '" ';
            if (isset($attr["id"])) {
                $string .= 'id="' . $attr["id"] . '" ';
            }
            if (isset($attr["class"])) {
                $string .= 'class="' . $attr["class"] . '" ';
            }
            if ($attr["disabled"]===true) {
                $string .= 'disabled ';
            }
            if ($attr["value"]==$option_value) {
                $string .= 'checked ';
            } 
            $string .= ' />';
        }
        
        if (!empty($attr["description"])) {
            $string .= '<div class="form-description">' . $attr["description"] . '</div>';
        }
        if ($attr["toString"]) {
            return $string;
        }
        else {
            echo $string;
        }
    }//End print_radio

    /**
     * Text area generating function
     *
     * @since 1.0
     * @author KeiMOon
     *
     * @param array $attr This parameter is an array containing following key: <br />
     * <b>format</b> (default=<u>table</u>, may be <u>div</u>)<br />
     * <b>name</b> : The name of the text area. This field is required <br />
     * <b>value</b> : The value of the text area. This field is optional <br />
     * <b>id</b> : The ID of the text area. This field is optional <br />
     * <b>class</b> : The class of the text area. This field is optional. Default
     * value is <u>form-textarea</u> <br />
     * <b>cols</b> : Number of columns of the text area. This field is optional. Default
     * value is 50 <br />
     * <b>rows</b> : Number of rows of the text area. This field is optional. Default
     * value is 10 <br />
     * <b>disabled</b> : Text area is disabled or not. Default value is <u>false</u>,
     * may be <u>true</u>) <br />
     * <b>readOnly</b> : Text area is read-only or not. Default value is <u>false</u>,
     * may be <u>true</u>) <br />
     * <b>description</b> : The description of the text area. This field is optional. Description will
     * be display under the form element. <br />
     * <b>toString</b> : Print to a string instead
     * @return bool
     */


    static function print_textarea($attr) {
        if (!isset($attr["name"])) {
            return false;
        }
        if (empty($attr["class"])) {
            $attr["class"] = "form-textarea";
        }
        if (empty($attr["cols"])) {
            $attr["cols"] = "50";
        }
        if (empty($attr["rows"])) {
            $attr["rows"] = "10";
        }
        
        $string = '<textarea name="' . $attr["name"] . '" ';
        if (isset($attr["id"])) {
            $string .= 'id="' . $attr["id"] . '" ';
        }
        if (isset($attr["class"])) {
            $string .= 'class="' . $attr["class"] . '" ';
        }
        if (isset($attr["cols"])) { 
            $string .= 'cols="' . $attr["cols"] . '" ';
        } 
        if (isset($attr["rows"])) { 
            $string .= 'rows="' . $attr["rows"] . '" ';
        } 
        if ($attr["disabled"]===true) {
            $string .= 'disabled ';
        }

        if ($attr["readOnly"]===true) {
            $string .= 'readOnly ';
        }
        $string .= '>';
        if (isset($attr["value"])) {
            $string .= $attr["value"];
        }
        $string .= '</textarea>';

        if (!empty($attr["description"])) {
            $string .= '<div class="form-description">' . $attr["description"] . '</div>';
        }
        if ($attr["toString"]) {
            return $string;
        }
        else {
            echo $string;
        }
    } //End print_textarea

    /**
     * File select generating function
     *
     * @since 1.0
     * @author KeiMOon
     *
     * @param array $attr This parameter is an array containing following key: <br />
     * <b>name</b> : The name of the file select. This field is required <br />
     * <b>value</b> : The value of the file select. The value must be the direct link to the file.
     *  This field is optional <br />
     * <b>label</b> : The label of the file select. 
     *  This field is optional <br />
     * <b>id</b> : The ID of the file select. This field is optional <br />
     * <b>class</b> : The class of the file select. This field is optional. Default
     * value is <u>form-fileselect</u> <br />
     * <b>disabled</b> : File select is disabled or not. Default value is <u>false</u>,
     * may be <u>true</u>) <br />
     * <b>description</b> : The ID of the file select. This field is optional. Description will
     * be display under the form element. <br />
     * <b>toString</b> : Print to a string instead
     * @return bool
     */

    static function print_file_select($attr) {
        if (!isset($attr["name"])) {
            return false;
        }
        if (empty($attr["class"])) {
            $attr["class"] = "form-fileselect";
        }

        $string = "";
        if (!empty($attr["value"])) { 
            $string .= '<div class="form-fileselect-label"><a href="' . $attr["value"] . '">' . $attr["label"] . '</a></div>';
        } 

        $string .= '<input type="file"
                           name="' . $attr["name"] . '" ';
        if (isset($attr["id"])) {
            $string .= 'id="' . $attr["id"] . '" ';
        }
        if (isset($attr["class"])) {
            $string .= 'class="' . $attr["class"] . '" ';
        }
        if ($attr["disabled"]===true) {
            $string .= 'disabled ';
        }
        $string .= '/>';
        
        if (!empty($attr["description"])) {
            $string .= '<div class="form-description">' . $attr["description"] . '</div>';
        }
        if ($attr["toString"]) {
            return $string;
        }
        else {
            echo $string;
        }
    } //End print_file_select

    /**
     * File image generating function
     *
     * @since 1.0
     * @author KeiMOon
     *
     * @param array $attr This parameter is an array containing following key: <br />
     * <b>name</b> : The name of the file image. This field is required <br />
     * <b>value</b> : The value of the file image. The value must be the link to the image.
     *  This field is optional <br />
     * <b>id</b> : The ID of the file image. This field is optional <br />
     * <b>class</b> : The class of the file image. This field is optional. Default
     * value is <u>form-fileimage</u> <br />
     * <b>disabled</b> : file image is disabled or not. Default value is <u>false</u>,
     * may be <u>true</u>) <br />
     * <b>description</b> : The ID of the file image. This field is optional. Description will
     * be display under the form element. <br />
     * <b>toString</b> : Print to a string instead
     * @return bool
     */

    static function print_file_image($attr) {
        if (!isset($attr["name"])) {
            return false;
        }
        if (empty($attr["class"])) {
            $attr["class"] = "form-fileimage";
        }

        if (empty($attr["width"])) {
            $attr["width"] = false;
        }

        $string = "";
        if (!empty($attr["value"])) { 
            $string.= '<div class="form-fileimage-image">' . Owl_Html::make_it_image($attr["value"], __("Old Image"), $attr["width"]) . '</div>';
        } 

        $string .= '<input type="file"
                           name="' . $attr["name"] . '" ';
        if (isset($attr["id"])) {
            $string .= 'id="' . $attr["id"] . '" ';
        }
        if (isset($attr["class"])) {
            $string .= 'class="' . $attr["class"] . '" ';
        }
        if ($attr["disabled"]===true) {
            $string .= 'disabled ';
        }
        $string .= '/>';

        if (!empty($attr["description"])) {
            $string .= '<div class="form-description">' . $attr["description"] . '</div>';
        }
        if ($attr["toString"]) {
            return $string;
        }
        else {
            echo $string;
        }
    } //End print_file_image

    /**
     * Button generating function
     *
     * @since 1.0
     * @author KeiMOon
     *
     * @param array $attr This parameter is an array containing following key: <br />
     * <b>type</b> : The type of button is submit or reset. Default value is <u>submit</u>,
     *  may be <u>reset</u>) <br />
     * <b>name</b> : The name of the button. This field is required <br />
     * <b>value</b> : The value of the button. This is the text displayed on the button.
     *  This field is optional <br />
     * <b>id</b> : The ID of the button. This field is optional <br />
     * <b>class</b> : The class of the button. This field is optional. Default
     * value is <u>form-button</u> <br />
     * <b>title</b> : The title of the button. This field is optional <br />
     * <b>disabled</b> : Button is disabled or not. Default value is <u>false</u>,
     * may be <u>true</u>) <br />
     * <b>description</b> : The description of the button. This field is optional. Description will
     * be display under the form element. <br />
     * <b>toString</b> : Print to a string instead
     * @return bool
     */

    static function print_button($attr) {
        if (!isset($attr["type"]) || $attr["type"]!="submit" && $attr["type"]!= "reset") {
            $attr["type"]="submit";
        }
        if (!isset($attr["name"])) {
            return false;
        }
        if (empty($attr["class"])) {
            $attr["class"] = "form-button";
        }

        $string = '<input type="' . $attr["type"] . '"
                          name="' . $attr["name"] . '" ';
        if (isset($attr["value"])) { 
            $string .= 'value="' . $attr["value"] . '" ';
        } 
        if (isset($attr["id"])) {
            $string .= 'id="' . $attr["id"] . '" ';
        }
        if (isset($attr["class"])) {
            $string .= 'class="' . $attr["class"] . '" ';
        }
        if (isset($attr["title"])) {
            $string .= 'title="' . $attr["title"] . '" ';
        }
        if ($attr["disabled"]===true) {
            $string .= 'disabled ';
        }
        $string .= '/>';

        if (!empty($attr["description"])) {
            $string .= '<div class="form-description">' . $attr["description"] . '</div>';
        }
        if ($attr["toString"]) {
            return $string;
        }
        else {
            echo $string;
        }
    } //End print_button

    /**
     * Label generating function
     *
     * @since 1.0
     * @author KeiMOon
     *
     * @param array $attr This parameter is an array containing following key: <br />
     * <b>value</b> : The value of the label. This is the text displayed on the label.
     *  This field is required <br />
     * <b>for</b> : Specifies which form element a label is bound to. This field
     * is optional<br />
     * <b>required</b> : Specifies the input is required<br />
     * <b>id</b> : The ID of the label. This field is optional <br />
     * <b>class</b> : The class of the label. This field is optional. Default
     * value is <u>form-label</u> <br />
     * <b>toString</b> : Print to a string instead
     * @return bool
     */
    static function print_label($attr) {
        if (!isset($attr["value"])) {
            return false;
        }
        if (empty($attr["class"])) {
            $attr["class"] = "form-label";
        }

        $string = '<label ';
        if (isset($attr["for"])) {
            $string .= 'for="' . $attr["for"] . '" ';
        }
        if (isset($attr["id"])) {
            $string .= 'id="' . $attr["id"] . '" ';
        }
        if (isset($attr["class"])) {
            $string .= 'class="' . $attr["class"] . '" ';
        }
        if ($attr["required"] == true) {
            $string .= '>' . $attr["value"] . '<span class="required"> (*)</span>' . '</label>';
        }
        else {
            $string .= '>' . $attr["value"] . '</label>';
        }

        if ($attr["toString"]) {
            return $string;
        }
        else {
            echo $string;
        }
        
    } //End print label

    /**
     * Hidden input generating function
     *
     * @since 1.0
     * @author KeiMOon
     *
     * @param array $attr This parameter is an array containing following key: <br />
     * <b>name</b> : The name of the text box. This field is required <br />
     * <b>value</b> : The value of the text box. This field is optional <br />
     * <b>id</b> : The ID of the text box. This field is optional <br />
     * <b>toString</b> : Print to a string instead
     * @return bool
     */

    static function print_hidden($attr) {
        if (empty($attr["name"])) {
            return false;
        }

        $string = '<input type="hidden"
                          name="' . $attr["name"] . '" ';

        if (isset($attr["value"])) {
            $string .= 'value="' . $attr["value"] . '" ';
        }

        if (isset($attr["id"])) {
            $string .= 'id="' . $attr["id"] . '" ';
        }
        if (isset($attr["class"])) {
            $string .= 'class="' . $attr["class"] . '" ';
        }
        $string .= ' />';
        if ($attr["toString"]) {
            return $string;
        }
        else {
            echo $string;
        }
} //End print_hidden

} //End class


